const app = getApp();
const http = require('../../../utils/http.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum1: 1,
    pageNum2: 1,
    pageNum3: 1,
    pageNum4: 1,
    pageSize: 15,
    isShow: false,
    send_coupon_params: [],
    sign: "",
    send_coupon_merchant: "",
    TabCur: 0,
    SortMenu: [{id: 0, name: "未领取"}, {id: 1, name: "未使用"}, {id: 2, name: "已使用"}, {id: 3, name: "已过期"}],
    couponList:[],
    testName: '',
    index:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      customer: app.globalData.customer
    })
    if(options.id){
      this.setData({
        TabCur: options.id
      })
      let postData = {status: options.id,pageNum: 1,pageSize: this.data.pageSize}
      this.selectCouponList(postData)
    }
    //this.selectCouponList({status: this.data.TabCur})
  },

  selectCouponList(data) {
    http.get('/api/user/myCoupon', data).then(res => {
      if (res.code == 200) {
        this.setData({
          couponList: res.data.list,
          hasNextPage: res.data.hasNextPage,
        })
        if(this.data.TabCur==0){
          this.setData({
            pageNum1:2
          })
        }
        if(this.data.TabCur==1){
          this.setData({
            pageNum2:2
          })
        }
        if(this.data.TabCur==2){
          this.setData({
            pageNum3:2
          })
        }
        if(this.data.TabCur==3){
          this.setData({
            pageNum4:2
          })
        }
      }
    })
  },

  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id
    })
    let postData = {status: e.currentTarget.dataset.id,pageNum: 1,pageSize: this.data.pageSize}
    this.selectCouponList(postData)
  },

  couponClick(e) {
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    //console.log(this.data.couponList)
    let coupon = this.data.couponList[index];
    
    if (coupon.status == 1) {
      console.log(coupon)
      wx.navigateTo({
        url: "/pages/user/mycoupon/qrcode?name=" + coupon.productName + "&code=" + coupon.couponCode + "&ks=" + coupon.availableBeginTime+ "&js=" + coupon.availableEndTime + "&time=" + coupon.useDate + "&sj=" + coupon.mobile
      })
    }
    else if (coupon.status == 0) {
      console.log(coupon)
      http.get("/api/purchase/getMerCouponSign", {couponOrderId: coupon.id}).then(res=>{
        if (res.code == 200) {
          this.setData({
            send_coupon_params: res.data.send_coupon_params,
            sign: res.data.sign,
            send_coupon_merchant: res.data.send_coupon_merchant,
            isShow: true,
            index:index,
            testName: coupon.productName
          })
        }
      })
    }
  },

  sendSuccess(params) {
    //console.log(params)
    let result = params.detail.send_coupon_result[0]
    let coupon = this.data.couponList[this.data.index];
    let list=this.data.couponList
    console.log(result)
    console.log(coupon)
    if (result.code=='SUCCESS'){
      this.setData({
        isShow: false
      })
      list.splice(this.data.index, 1)
      this.setData({
        couponList:list
      })
      http.get("/api/purchase/endCouponSuccess", {couponCode: result.coupon_code,outTradeNo: coupon.outTradeNo}).then(res=>{
        if (res.code == 200) {
          wx.navigateTo({
            url: "/pages/user/mycoupon/qrcode?name=" + coupon.productName + "&code=" + result.coupon_code + "&ks=" + coupon.availableBeginTime+ "&js=" + coupon.availableEndTime + "&time=" + coupon.useDate + "&sj=" + coupon.mobile
          })
        }
      })
    }else {
      wx.showToast({
        title: result.message,
        icon: 'none',
        duration: 2000
      })
    }
    console.log(result)
    
  },

  hideModal(e) {
    this.setData({
      isShow: false
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.selectCouponList({status: this.data.TabCur,pageNum: 1,pageSize: this.data.pageSize})
  },

  /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      console.log("上拉分页")
      var TabCur = this.data.TabCur
      var url, listTemp, pageNum, list
      list=this.data.couponList
      const that = this
      wx.showLoading({
          title: '加载中。。。',
          mask: true
      })
      if (TabCur == 0) {
          pageNum = that.data.pageNum1;
      } 
      if (TabCur == 1) {
          pageNum = that.data.pageNum2;
      } 
      if (TabCur == 2) {
          pageNum = that.data.pageNum3;
      }
      if (TabCur == 3) {
          pageNum = that.data.pageNum4;
      }
      if (this.data.hasNextPage) {
          http.get('/api/user/myCoupon', {status: this.data.TabCur,pageNum: pageNum, pageSize: this.data.pageSize}).then(res => {
              wx.hideLoading()
              if (res.code == 200) {
                  if (TabCur == 0) {
                      this.setData({
                          pageNum1: res.data.pageNum + 1,
                          hasNextPage: res.data.hasNextPage,
                          couponList: list.concat(res.data.list)
                      })
                  }
                  if (TabCur == 1) {
                      this.setData({
                          pageNum2: res.data.pageNum + 1,
                          hasNextPage: res.data.hasNextPage,
                          couponList: list.concat(res.data.list)
                      })
                  }
                  if (TabCur == 2) {
                      this.setData({
                          pageNum3: res.data.pageNum + 1,
                          hasNextPage: res.data.hasNextPage,
                          couponList: list.concat(res.data.list)
                      })
                  }
                  if (TabCur == 3) {
                      this.setData({
                          pageNum4: res.data.pageNum + 1,
                          hasNextPage: res.data.hasNextPage,
                          couponList: list.concat(res.data.list)
                      })
                  }
              }
          })
      } else {
          wx.showToast({
              title: '没有更多数据',
              icon: "none"
          })
      }
  }

})
